//
//  productDetailsViewController.m
//  EcShop
//
//  Created by soft on 2017/11/7.
//  Copyright © 2017年 soft. All rights reserved.
//

#import "productDetailsViewController.h"
#import "DetailsCollectionViewCell.h"
@interface productDetailsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collView;
@end

@implementation productDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.prodtD.name;
   
    self.view.backgroundColor = [UIColor yellowColor];
     [self productView];
    // Do any additional setup after loading the view.
}
-(void)productView{
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*0.9)];
    scrollView.center = CGPointMake(self.view.bounds.size.width*0.5, self.view.bounds.size.height*0.45);
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width,self.prodtD.pics.count*self.view.frame.size.height*0.4 +160);
    [self.view addSubview:scrollView];
    
     UILabel *price=[[UILabel alloc] initWithFrame:CGRectMake(0, 10,scrollView.bounds.size.width, 20)];//设定位置与大小
    price.textColor = [UIColor redColor];
    price.font = [UIFont systemFontOfSize:40];
    price.text = [NSString stringWithFormat:@"￥:%@",self.prodtD.price];
    [scrollView addSubview:price];
    
    UILabel *Details=[[UILabel alloc] initWithFrame:CGRectMake(0, 35,scrollView.bounds.size.width, 80)];//设定位置与大小
    Details.textColor = [UIColor blackColor];
    //Details.font = [UIFont systemFontOfSize:40];
    Details.text =self.prodtD.descriptions[@"desc"];
    Details.numberOfLines = 3;
    [scrollView addSubview:Details];
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //设置headerView的尺寸大小
    //layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
    //该方法也可以设置itemSize
    //layout.itemSize =CGSizeMake(110, 150);
    
    //2.初始化collectionView
    self.collView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 160, scrollView.bounds.size.width, scrollView.bounds.size.height) collectionViewLayout:layout];
    [scrollView addSubview:self.collView];
    self.collView.backgroundColor = [UIColor clearColor];
    
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [self.collView registerClass:[DetailsCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [self.collView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.设置代理
    self.collView.delegate = self;
    self.collView.dataSource = self;
    
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*0.1)];
    footView.center = CGPointMake(self.view.bounds.size.width*0.5, self.view.bounds.size.height*0.95);
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UIButton *buyBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, footView.bounds.size.width*0.3, footView.bounds.size.height*0.6)];
    buyBtn.center = CGPointMake(footView.bounds.size.width*0.8, footView.bounds.size.height*0.5);
    [buyBtn setTitle:@"购买" forState:UIControlStateNormal];
   // [buyBtn setTintColor:[UIColor whiteColor]];
    buyBtn.backgroundColor = [UIColor redColor];
    [footView addSubview:buyBtn];
    UIButton *collectBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, footView.bounds.size.width*0.2, footView.bounds.size.height*0.6)];
    collectBtn.center = CGPointMake(footView.bounds.size.width*0.3, footView.bounds.size.height*0.5);
    [collectBtn setTitle:@"搜藏" forState:UIControlStateNormal];
    [collectBtn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [footView addSubview:collectBtn];

    UIButton *messageBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, footView.bounds.size.width*0.2, footView.bounds.size.height*0.6)];
    messageBtn.center = CGPointMake(footView.bounds.size.width*0.1, footView.bounds.size.height*0.5);
    [messageBtn setTitle:@"留言" forState:UIControlStateNormal];
    [messageBtn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [footView addSubview:messageBtn];
    
    
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.prodtD.pics.count;
    NSLog(@"%lu",(unsigned long)self.prodtD.pics.count);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailsCollectionViewCell *cell = (DetailsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
    //cell.botlabel.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];
    
    cell.backgroundColor = [UIColor yellowColor];
    [self download:cell ImageWithUrl:self.prodtD.pics[indexPath.row][@"url"]];
    
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.bounds.size.width*0.95, 240);
}

//footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}

//header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}

//设置每个item的UIEdgeInsets
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(10, 10, 10, 10);
//}

////设置每个item水平间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 10;
//}
//
//
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}


//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
//    headerView.backgroundColor =[UIColor grayColor];
//    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
//    label.text = @"这是collectionView的头部";
//    label.font = [UIFont systemFontOfSize:20];
//    [headerView addSubview:label];
//    return headerView;
//}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    DetailsCollectionViewCell *cell = (DetailsCollectionViewCell *)[collectionView cellForItemAtIndexPath:[indexPath]];
//    NSString *msg = cell.botlabel.text;
//    NSLog(@"%@",msg);
}


//异步线程加载网络下载图片 ——> 回到主线程更新UI
-(void)download:(DetailsCollectionViewCell *)cell ImageWithUrl:(NSString *)imageDownloadURLStr{
    //以便在block中使用
    __block UIImage *image = [[UIImage alloc] init];
    //图片下载链接
    NSString *url = [NSString stringWithFormat:@"https://cdnme.azurewebsites.net%@",imageDownloadURLStr];
    NSURL *imageDownloadURL = [NSURL URLWithString:url];
    
    //将图片下载在异步线程进行
    //创建异步线程执行队列
    dispatch_queue_t asynchronousQueue = dispatch_queue_create("imageDownloadQueue", NULL);
    //创建异步线程
    dispatch_async(asynchronousQueue, ^{
        //网络下载图片  NSData格式
        NSError *error;
        NSData *imageData = [NSData dataWithContentsOfURL:imageDownloadURL options:NSDataReadingMappedIfSafe error:&error];
        if (imageData) {
            image = [UIImage imageWithData:imageData];
        }
        //回到主线程更新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            // [_imageView setImage:image];
            cell.productImage.image = image;
        });
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
